package com.clq.reflect;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Random;

import com.clq.service.TestService;
import com.clq.test.Cat;

public class ReflectDemo implements InvocationHandler{
	
	private Object proxy;
	private Object target;
	
	public Object bind(Object proxy,Object target) throws InstantiationException, IllegalAccessException {
		this.proxy = proxy;
		this.target = target;
		return Proxy.newProxyInstance(this.target.getClass().getClassLoader(),new Class[] {TestService.class},this);
	}
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		//反射得到操作者的实例
		Class clazz = this.target.getClass();
		//反射得到操作者的start的方法
		Method start = clazz.getDeclaredMethod("say",null);
		start.invoke(this.target,null);
		method.invoke(this.proxy,null);
		Method end = clazz.getDeclaredMethod("say2",null);
		end.invoke(this.target,null);
		return null;
	}
	
	public static void main(String[] args) throws InstantiationException, IllegalAccessException {
		ReflectDemo reflectDemo  = new ReflectDemo();
		TestService t = (TestService) reflectDemo.bind(new ITest(),new Cat());
		t.test();
	}
}


class ITest implements TestService{
	public void test(){
		System.out.println("test~~~~~~~");
	}
}